package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    // 增添数据
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    // 更新数据
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    // 删除单个数据
    @DeleteMapping("/deletById/{userId}")
    public Result deletById(@PathVariable Long userId) {
        userService.deletById(userId);
        return Result.success();
    }

    // 查询所有数据
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> list = userService.selectAll();
        return Result.success(list);
    }

    // 查询单个数据
    @GetMapping("/selectById/{userId}")
    public Result selectById(@PathVariable Long userId) {
        User user = userService.selectById(userId);
        return Result.success(user);
    }

    // 分页查询
    // pageNum: 当前页码
    // pageSize: 每页的个数
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "9") Integer pageSize) {
        PageInfo<User> pageInfo = userService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}